﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Quan_ly_thong_tin_he_thong
{
    public partial class LoaiSanPham : Form
    {
        public string filename;
        public string tenanh;
        string MSLSP;
        public LoaiSanPham()
        {
            InitializeComponent();
        }

        private void LoaiSanPham_Load(object sender, EventArgs e)
        {
            LoadDataGridView1();
            comboBoxMSCL();
        }
        private void LoadDataGridView1()
        {
            Config file = new Config(Application.StartupPath + "\\Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "Select MaSo_LSP,Ten_LSP,HinhAnh_LSP,Ten_CL from LoaiSanPham join ChatLieu on LoaiSanPham.MaSo_CL=ChatLieu.MaSo_CL";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            conn.Open();

            DataSet ds = new DataSet();
            adapter.Fill(ds, "LoaiSanPham");
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = ds.Tables["LoaiSanPham"];
            conn.Close();
        }

        private void buttonExitLSP_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonAddLSP_Click(object sender, EventArgs e)
        {
            ThemLSP();
            LoadDataGridView1();
            
        }
        private void ThemLSP()
        {
            if (txtMSLSP.Text.Length == 0 || txtTLSP.Text.Length == 0)
                TBTCongLSP.Text = "Thêm mới loại sản phẩm không thành công";
            else
            {
                Config file = new Config(Application.StartupPath + "\\Config.ini");
                string connectionstring = file.ReadValue("server", "con");
                SqlConnection conn = new SqlConnection(connectionstring);
                string sql = "insert into LoaiSanPham(MaSo_LSP,Ten_LSP,MaSo_CL,HinhAnh_LSP) values (@MaSo_LSP,@Ten_LSP,@MaSo_CL,@HinhAnh_LSP)";
                SqlCommand cmd = new SqlCommand(sql, conn);
                conn.Open();
                cmd.Parameters.AddWithValue("@MaSo_LSP", txtMSLSP.Text);
                cmd.Parameters.AddWithValue("@Ten_LSP", txtTLSP.Text);
                cmd.Parameters.AddWithValue("@MaSo_CL", comMSCL.SelectedValue);
                cmd.Parameters.AddWithValue("@HinhAnh_LSP", tenanh);
                cmd.ExecuteNonQuery();
                conn.Close();
                TBTCongLSP.Text = "Thêm mới loại sản phẩm thành công ";
            }
        }

        private void buttonUpdateLSP_Click(object sender, EventArgs e)
        {
            CapNhapLSP(MSLSP);
            LoadDataGridView1();
            
        }
        private void CapNhapLSP(string MSLSP)
        {
            if (txtMSLSP.Text.Length == 0 || txtTLSP.Text.Length == 0)
                TBTCongLSP.Text = "Cập nhật loại sản phẩm không thành công";
            else
            {
                Config file = new Config(Application.StartupPath + "\\Config.ini");
                string connectionstring = file.ReadValue("server", "con");
                SqlConnection conn = new SqlConnection(connectionstring);
                string sql = "update LoaiSanPham set Ten_LSP=@Ten_LSP,MaSo_CL=@MaSo_CL ,HinhAnh_LSP=@HinhAnh_LSP where MaSo_LSP=@MaSo_LSP";
                SqlCommand cmd = new SqlCommand(sql, conn);
                conn.Open();
                cmd.Parameters.AddWithValue("@MaSo_LSP", MSLSP);
                cmd.Parameters.AddWithValue("@Ten_LSP", txtTLSP.Text);
                cmd.Parameters.AddWithValue("@MaSo_CL", comMSCL.SelectedValue);
                cmd.Parameters.AddWithValue("@HinhAnh_LSP", tenanh);
                cmd.ExecuteNonQuery();
                conn.Close();
                TBTCongLSP.Text = "Cập nhật loại sản phẩm thành công";
            }
        }

        private void buttonDelLSP_Click(object sender, EventArgs e)
        {
            DeleteLSP(MSLSP);
            LoadDataGridView1();
        }
        private void DeleteLSP(string MSLSP)
        {
            if (txtMSLSP.Text.Length == 0 || txtTLSP.Text.Length == 0)
                TBTCongLSP.Text = "Xóa loại sản phẩm không thành công";
            else
            {
                Config file = new Config(Application.StartupPath + "\\Config.ini");
                string connectionstring = file.ReadValue("server", "con");
                SqlConnection conn = new SqlConnection(connectionstring);
                string sql = "delete from LoaiSanPham where MaSo_LSP = @MaSo_LSP";
                SqlCommand cmd = new SqlCommand(sql, conn);
                conn.Open();
                cmd.Parameters.AddWithValue("@MaSo_LSP", MSLSP);
                cmd.ExecuteNonQuery();
                conn.Close();
                TBTCongLSP.Text = "Xóa loại sản phẩm thành công";
            }
        }

        private void buttonThemAnh_Click(object sender, EventArgs e)
        {
            OpenFileDialog f = new OpenFileDialog();
            f.InitialDirectory = @"D:\9.LIBRARY DOCUMENTS\NAM II\VATC_HK III\THIẾT KẾ WEB\THUYET TRINH WEB\Nhom M3T - Lop 5103VIP-Finish\images";
            f.Filter = "Images Files (*.bmp, .*jpg, *.png)|*.bmp; *.jpg; *.png|All Files (*.*)|*.*";
            if (f.ShowDialog() == DialogResult.OK)
            {
                filename = f.FileName;
                tenanh = System.IO.Path.GetFileName(f.FileName);
                pictureBox1.Image = Image.FromFile(filename);
            }
        }

        private void buttonXoaAnh_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = null;
        }

        private void comboBoxMSCL()
        {
            Config file = new Config(Application.StartupPath + "\\Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * from ChatLieu";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            conn.Open();
            
            DataSet ds = new DataSet();
            adapter.Fill(ds, "ChatLieu");
            DataTable dt = ds.Tables["ChatLieu"];
            comMSCL.DisplayMember = "Ten_CL";
            comMSCL.ValueMember = "MaSo_CL";
            comMSCL.DataSource = ds.Tables["ChatLieu"].DefaultView;
            conn.Close();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            
            MSLSP = dataGridView1.Rows[e.RowIndex].Cells["MaSo_LSP"].Value.ToString();
            loadLSP(MSLSP);

        }
        private void loadLSP(string MSLSP)
        {
            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * from LoaiSanPham where MaSo_LSP = @MaSo_LSP";
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            cmd.Parameters.AddWithValue("@MaSo_LSP", MSLSP);
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                txtMSLSP.Text= reader["MaSo_LSP"].ToString();
                txtTLSP.Text = reader["Ten_LSP"].ToString();
                string duongdan = file.ReadValue("duongdanhinhanh", "hinhanh");
                tenanh = reader["HinhAnh_LSP"].ToString();
                pictureBox1.Image = Image.FromFile(duongdan+tenanh);
                comMSCL.SelectedValue = reader["MaSo_CL"].ToString();
                
            }
            reader.Close();
            conn.Close();
        }

        private void butClearLSP_Click(object sender, EventArgs e)
        {
            txtMSLSP.Text = null;
            txtTLSP.Text = null;
            pictureBox1.Image = null;
            TBTCongLSP.Text = null;
            TBTCongLSP.Text = null;
        }

       

       


       

       
       

       

       

       
    }
}
